/**
 * @author xiaobyu
 * @created Mar 30, 2016
 * 
 */
package nconv.wiki;

import nconv.convert.ImageHandler;
import nconv.utils.LogCenter;
import nconv.utils.LogCenter.DOC_OPERATION;
import org.wikipedia.Mediawiki;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MediaWikiProxy {
	
	Mediawiki mw = null;
	public static enum WikiSite {
		AUTO_WIKI,
		SPEC_WIKI
	}

	private static Map<WikiSite, MediaWikiProxy> map = new HashMap<>();

	public static void registerWikiSite(WikiSite site, MediaWikiProxy proxy) {
		map.put(site, proxy);
	}

	public static MediaWikiProxy getWikiSite(WikiSite site) {
		return map.get(site);
	}

	private boolean noUpdateForSameContent = true;

	static {
		registerWikiSite(WikiSite.AUTO_WIKI, new MediaWikiProxy("http://10.113.4.62:5800/autowiki", "Robot", "lyu12345"));
		//registerWikiSite(WikiSite.AUTO_WIKI, new MediaWikiProxy("http://10.0.0.30:80/mediawiki", "Lambert Yu", "lyu12345"));
	}

	public MediaWikiProxy(String url, String name, String password) {
		mw = new Mediawiki();
		try {
			mw.setSiteurl(url);
			mw.login(name, password);
			mw.getWiki().setThrottle(500);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setNoUpdateForSameContent(boolean noUpdateForSameContent) {
		this.noUpdateForSameContent = noUpdateForSameContent;
	}
	
	public boolean hasPage(String title) throws Exception {
		return mw.getPageContent(title) != null;
	}
	
	public void deletePage(String title, String reason) throws Exception {
		mw.delete(title, reason);
		LogCenter.logWiki(title, DOC_OPERATION.DELETE);
	}

	public String getPageContent(String title) throws Exception {
		return mw.getPageContent(title);
	}
	
	public void createPage(String pageTitle, String text, String summary) throws Exception {
		DOC_OPERATION oper = DOC_OPERATION.UPDATE;

		text = text.trim().replaceAll("\\r\\n?", "\n") + "\n";

		if (noUpdateForSameContent) {
			String pageContent = getPageContent(pageTitle);
			if (pageContent == null) {
				oper = DOC_OPERATION.CREATE;
			}

			if (text.equals(pageContent)) return;
		}
		
		mw.edit(pageTitle, text, summary);
		LogCenter.logWiki(pageTitle, oper);
	}
	
	public void uploadFile(String fileName, String wikiFileName) throws Exception {
		uploadFile(new File(fileName), wikiFileName);
	}
	
	public void uploadFile(File file, String wikiFileName) throws Exception {
		uploadFile(file, wikiFileName, "", "Upload a file");
	}
	
	public void uploadFile(String fileName, String wikiFileName, String text, String reason) throws Exception {
		uploadFile(new File(fileName), wikiFileName, text, reason);
	}
	
	public void uploadFile(File file, String wikiFileName, String text, String reason) throws Exception {
		Map<String, Object> metaMap = mw.getFileMetadata(wikiFileName);
		if (ImageHandler.isSameFile(metaMap, file)) return;

		mw.upload(file, wikiFileName, text, reason);
		String lastUploaded = mw.getLastSuccessUploadFileName();
		boolean success = true;
		if (lastUploaded == null) {
			lastUploaded = wikiFileName;
			success = false;
		}
		LogCenter.logWiki("Media:" + lastUploaded + "|" + lastUploaded,
				metaMap == null ? DOC_OPERATION.CREATE : DOC_OPERATION.UPDATE, success ? "" : "Failed");
	}
}
